class MonitorEntry < ActiveRecord::Base
  attr_accessible :date, :time_zone, :user_uuid, :log_class, :status, :type_ 
  
  def self.json_deserializer! json
    MonitorEntry.new({ :user_uuid => json[:user_uuid],
                       :date => DateTime.parse(Time.at(json[:date] / 1000).to_s),
                       :time_zone => json[:timeZone],
                       :status => json[:status],
                       :type_ => json[:type], 
                       :log_class => json[:logClass] })
  end
  
  def self.get_user_participation_total_days user_uuid
    monitor_entries = MonitorEntry.where(:user_uuid => user_uuid).order("date")
    last_day = monitor_entries.last.date
    first_day = monitor_entries.first.date
    (last_day - first_day).to_i/86400
  end
  
  def self.get_user_events_quantity user_uuid, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    user_entries.where("date > ?", date_limiar).count
  end

  def self.get_user_events_quantity_by_log_class user_uuid, log_class, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    user_entries.where("log_class = ? AND date > ?", log_class, date_limiar).count
  end

  def self.get_user_events_quantity_by_log_class_and_status user_uuid, log_class, status, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    user_entries.where("log_class = ? AND status = ? AND date > ?", log_class, status, date_limiar).count
  end

  def self.get_user_battery_events_quantity_by_type user_uuid, _type, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    user_entries.where("log_class = ? AND status = ? AND type_ = ? AND date > ?", 
                        MonitorEntry.BATTERY_ENTRY, MonitorEntry.POWER_CONNECTED, _type, date_limiar).count
  end
  
  def self.get_user_connectivity_events_quantity_by_type user_uuid, _type, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    if _type == MonitorEntry.NET_TYPE_MOBILE
      user_entries.where("log_class = ? AND status = ? AND type_ > -1 AND type_ < 6 AND type_ <> 1 AND date > ?", 
                                              MonitorEntry.CONNECTIVITY_ENTRY, MonitorEntry.NET_CONNECTED, date_limiar).count
    elsif _type == MonitorEntry.NET_TYPE_WIFI
      user_entries.where("log_class = ? AND status = ? AND type_ = 1 AND date > ?", 
                                              MonitorEntry.CONNECTIVITY_ENTRY, MonitorEntry.NET_CONNECTED, date_limiar).count
    end
  end
  
  def self.not_charging_duration_by_time_data user_uuid, threshold, days_ago
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    battery_events = user_entries.where("(log_class = ? OR (log_class = ? AND status = ?)) AND date > ?", 
                          MonitorEntry.BATTERY_ENTRY, MonitorEntry.OS_ENTRY, MonitorEntry.OS_SHUTDOWN, date_limiar).order("date")  
    data_table = []
    interval_begin = nil
    
    battery_events.each do |entry|
      if entry.status == MonitorEntry.POWER_DISCONNECTED || (entry.log_class == MonitorEntry.OS_ENTRY && entry.status == MonitorEntry.OS_SHUTDOWN)
        if interval_begin.nil?
          interval_begin = entry.date
        end
      elsif !interval_begin.nil?
        length = (entry.date-interval_begin).to_i/60
        if length >= threshold.to_i
          data_table.push length
        end
        interval_begin = nil
      end
    end
    
    return data_table
  end
  
  def self.no_connectivity_duration_by_time_data user_uuid, threshold, days_ago, net_type
    user_entries = MonitorEntry.where("user_uuid = ?", user_uuid)
    last_event_date = user_entries.last.date
    date_limiar = last_event_date - days_ago.to_i.days
    
    connectivity_events = nil
    if net_type.to_i == MonitorEntry.NET_TYPE_MOBILE
      connectivity_events = user_entries.where("(log_class = ? OR (log_class = ? AND status = ?)) AND type_ > -1 AND type_ < 6 AND type_ <> 1 AND date > ?", 
                            MonitorEntry.CONNECTIVITY_ENTRY, MonitorEntry.OS_ENTRY, MonitorEntry.OS_SHUTDOWN, date_limiar).order("date")
    elsif net_type.to_i == MonitorEntry.NET_TYPE_WIFI
      connectivity_events = user_entries.where("(log_class = ? OR (log_class = ? AND status = ?)) AND type_ = 1 AND date > ?", 
                            MonitorEntry.CONNECTIVITY_ENTRY, MonitorEntry.OS_ENTRY, MonitorEntry.OS_SHUTDOWN, date_limiar).order("date")
    end
    
    data_table = []
    interval_begin = nil
    connectivity_events.each do |entry|
      if entry.status == MonitorEntry.NET_DISCONNECTED || (entry.log_class == MonitorEntry.OS_ENTRY && entry.status == MonitorEntry.OS_SHUTDOWN)
        if interval_begin.nil?
          interval_begin = entry.date
        end
      elsif !interval_begin.nil?
        length = (entry.date-interval_begin).to_i/60
        if length >= threshold.to_i
          data_table.push length
        end
        interval_begin = nil
      end
    end
    
    return data_table
  end
  
  def self.EMPTY_ENTRY
    -1
  end
  
  def self.BATTERY_ENTRY
    0
  end
   
  def self.CONNECTIVITY_ENTRY
    1
  end
  
  def self.OS_ENTRY
    2
  end
  
  def self.OS_SHUTDOWN
    0
  end
  
  def self.OS_BOOT
    1
  end
  
  def self.POWER_CONNECTED
    1
  end
  
  def self.POWER_DISCONNECTED
    0
  end
  
  def self.POWER_TYPE_USB
    2
  end
  
  def self.POWER_TYPE_AC
    1
  end
  
  def self.NET_CONNECTED
    1
  end
  
  def self.NET_DISCONNECTED
    0
  end
  
  def self.NET_TYPE_MOBILE
    1
  end
  
  def self.NET_TYPE_WIFI
    0
  end
end
